<?php
$session = Mage::getSingleton('admin/session');
$user = $session->getUser();
$websiteId = $user->getWebsiteId();
$store = Mage::getModel('core/store');
if($websiteId)
{
	$website = new Mage_Core_Model_Website();
    $website->load($websiteId);
    $store = $website->getDefaultStore();
}


$orderIds = $this->getRequest()->getPost('order_ids');
$_coreHelper = $this->helper('core');
$collection = Mage::getModel('sales/order')->getCollection()
	->addFieldToSelect(array('entity_id','customer_id'))
	->addFieldToFilter('status', array('neq'=>'canceled'))
	->addFieldToFilter('entity_id', array('in'=>$orderIds));

$orders = array();
foreach($collection as $item)
{
	$orders[$item->getCustomerId()][] = $item->getId();
}
?>

<div class="print">

	<?php if($websiteId == 10):?>
		<style>
		* {font-family: "微软雅黑"}
		body {color:#000000;font-weight:bold;font:12px/1.5em Arial, Helvetica, sans-serif}
		th,td { color:#000000 !important;  border-color:#000000 !important; }
		.clearfix {clear:both;font-size:0px;line-height:0px;}
		.print-warehouse {}
		.print-warehouse .head {text-align:center;font-weight:bold;font-size:20pt;border:none;margin:1px auto;text-align: center !important;}
		.print-warehouse .title {line-height: 200%; font-size:14pt;}
		.print-warehouse .fl {float:left;}
		.print-warehouse .fr {float:right;}
		.print-warehouse .address-bar {line-height: 150%;font-size:14pt;}
		.address-bar span {float:left;margin-left:20px;}
		.data-table tr td {line-height: 120%; text-align: center !important;}
		.print .signature .item span.label {text-align: right !important;}
		p {font-size:12pt;line-height: 120%}
		</style>
		<?php foreach($orders as $customerId=>$orderIds):?>
		<div class="print-warehouse" style="page-break-after:always">
			<div class="title">
				<div class="fl">企业注册号：<?php echo Mage::getStoreConfig('general/website/company_no', $store);?></div>
				<div class="fr">食品流通许可证号：<?php echo Mage::getStoreConfig('general/website/company_permit', $store);?><a href="javascript:window.print();" class="actions"> 打印</a></div>
			</div>
			<div class="clearfix"></div>
			<div class="head">
					<?php if(Mage::getStoreConfig('general/website/company_warehouse_print_title', $store)):?>
						<?php echo Mage::getStoreConfig('general/website/company_warehouse_print_title', $store);?>
					<?php else:?>
						<?php echo '出库单';?>
					<?php endif;?>
			</div>
			<?php $order = Mage::getModel('sales/order')->load($orderIds[0]);?>

			<?php $customer = Mage::getModel('customer/customer')->load($order->getCustomerId());?>
			<div class="title address-bar">
				<span style="margin-left:0px"><strong>购货单位：</strong><?php echo $customer->getLastname();?></span>
				<span><strong>联系人：</strong><?php echo $order->getShippingAddress()->getFirstname();?></span>
				<span><strong>电话：</strong><?php echo $order->getShippingAddress()->getTelephone();?></span>
			</div>
			<div class="clearfix"></div>
			<?php
				$storeInfo = Mage::helper('qslb')->getStore($customer->getId());
				//var_dump($store);
				//exit();
			?>
			<div class="title address-bar">
				<span  style="margin-left:0px"><strong>地址：</strong><?php echo $order->getShippingAddress()->getStreetFull();?>
				<?php 
				if($storeInfo && $storeInfo['addressDescription']) echo '（'.$storeInfo['addressDescription'].'）';
				?>
				</span>
			</div>
			<div class="clearfix"></div>

			<?php $total = 0;?>
			<?php foreach($orderIds as $id):?>
			<?php $order = Mage::getModel("sales/order")->load($id);?>
			<?php 
				$prevOrders = Mage::getModel("sales/order")->getCollection()
					->addFieldToFilter('customer_id', array('eq'=>$customerId))
					->addFieldToFilter('state', array('in'=>array('complete','processing','new')))
					->addFieldToFilter('created_at',array('lt'=>$order->getCreatedAt()))
					->addFieldToFilter('entity_id', array('neq'=>$id));
					//echo count($prevOrders);
			?>
			<?php $createdAt = strtotime($order->getCreatedAt()); ?>
				<?php if($orderIds[0]!=$id):?>
				<div class="title address-bar">&nbsp;</div>
				<?php endif;?>
				<div class="title address-bar">
					<span style="margin-left:0px;font-size:18pt"><!-- <img src="<?php //echo $this->getUrl('admin_zgb/br/render',array('code'=>$order->getIncrementId(),'text'=>1))?>"/> --><strong>订单编号：</strong>#<?php echo $order->getIncrementId();?>&nbsp;&nbsp;<?php if(count($prevOrders)==0) echo "***首次下单***";?></span>
					<span><strong>销货日期：</strong><?php echo date("Y年m月d日 H:i", $createdAt+3600*8);?></span>
				</div>
				<div class="clearfix"></div>
				<?php $items = $order->getItemsCollection();?>
				<table class="data-table" style="margin-top:0px;">
					<tr class="th">
						<td width="13%">商品条码</td>
						<td>商品名称/规格</td>
						<td>单位</td>
						<td>数量</td>
						<td width="12%">单价（元）</td>
						<td width="12%">金额（元）</td>
						<!-- <td>生产单位</td> -->
						<td width="10%">生产日期<br/>（批号）</td>
						<td width="8%">保质期</td>
					</tr>
					<?php foreach($items as $item):?>
					<?php $product = Mage::getModel('catalog/product')->load($item->getProductId());?>
					<tr>
						<td><?php echo $product->getBarcode(); ?></td>
						<td style="text-align: left !important;"><?php if($item->getGiftId()) echo "**赠品**";?><?php echo $item->getName(); ?></td>
						<td><?php echo $product->getAttributeText('unit'); ?></td>
						<td><?php echo intval($item->getQtyOrdered()); ?></td>
						<?php if($item->getGiftId()):?>
						<td><?php echo $_coreHelper->formatPrice(0); ?></td>
						<td><?php echo $_coreHelper->formatPrice(0); ?></td>
						<?php else:?>
						<td><?php echo $_coreHelper->formatPrice($item->getPrice()); ?></td>
						<td><?php echo $_coreHelper->formatPrice($item->getRowTotal()); ?></td>
						<?php endif;?>
						<!-- <td><?php //echo $product->getManufacturer();?></td> -->
						<td></td>
						<td></td>
					</tr>
					<?php endforeach;?>
					<tr>
						<td colspan="6" style="text-align:right !important;">合计：</td>
						<td colspan="2"><?php echo $_coreHelper->formatPrice($order->getGrandTotal(),false); ?></td>
					</tr>
				</table>
			<?php 
				$order->setPrint(1);
				//$order->setStatus('pending_processing');
				$order->save();
			?>
			<?php $total = ($total + $order->getGrandTotal()); ?>
			<?php endforeach;?>
			<?php if(count($orderIds)>1):?>
			<table class="data-table" style="margin-top:10px;">
				<tr class="th" style="line-height:0px;height:0px !important;">
					<td width="13%" style="line-height:0px;height:0px !important;padding:0;font-size:0;border-left:none;border-right:none;border-top:none">商品条码</td>
					<td style="line-height:0px;height:0px !important;padding:0;font-size:0;border-left:none;border-right:none;border-top:none">商品名称/规格</td>
					<td style="line-height:0px;height:0px !important;padding:0;font-size:0;font-size:0;border-left:none;border-right:none;border-top:none">单位</td>
					<td style="line-height:0px;height:0px !important;padding:0;font-size:0;border-left:none;border-right:none;border-top:none">数量</td>
					<td width="12%" style="line-height:0px;height:0px !important;padding:0;font-size:0;border-left:none;border-right:none;border-top:none">单价（元）</td>
					<td width="12%" style="line-height:0px;height:0px !important;padding:0;font-size:0;border-left:none;border-right:none;border-top:none">金额（元）</td>
					<!-- <td>生产单位</td> -->
					<td width="10%" style="line-height:0px;height:0px !important;font-size:0;border-left:none;border-right:none;border-top:none">生产日期<br/>（批号）</td>
					<td width="8%" style="line-height:0px;height:0px !important;font-size:0;border-left:none;border-right:none;border-top:none">保质期</td>
				</tr>
				<tr>
					<td colspan="6" style="text-align:right !important;">总计：</td>
					<td colspan="2"><?php echo $_coreHelper->formatPrice($total, false); ?></td>
				</tr>
			</table>
			<?php endif;?>
			<div class="signature" style="clear:both">
				<div class="item"><span class="label">配货人：</span><span class="placeholder">&nbsp;</span></div>
				<div class="item"><span class="label">送货人：</span><span class="placeholder">&nbsp;</span></div>
				<div class="item"><span class="label">财务：</span><span class="placeholder">&nbsp;</span></div>
				<div class="item"><span class="label">收货人：</span><span class="placeholder">&nbsp;</span></div>
			</div>
			<div class="address-bar" style="margin-top:10px">
				<span style="margin-left:0px;">供货单位地址：<?php echo Mage::getStoreConfig('general/website/company_address', $store);?></span>
				<span>电话：<?php echo Mage::getStoreConfig('general/website/company_phone', $store);?></span>
			</div>
			<div class="clearfix"></div>
			<div style="margin-top:10px;font-size:6pt;line-height:120%">
				<p><?php echo Mage::getStoreConfig('general/website/company_warehouse_print_label', $store);?></p>
			</div>
		</div>
		<?php endforeach;?>
	<?php else:?>
	<?php foreach($orders as $customerId=>$orderIds):?>
		<style>
		* {font-family: "微软雅黑"}
		body {color:#000000;font-weight:bold;font:12px/1.5em Arial, Helvetica, sans-serif}
		th,td { color:#000000 !important;  border-color:#000000 !important; }
		</style>
		<div class="order-print">
		<div class="head" style="text-align: center !important;font-weight:bold;font-size:20pt;">送货单</div>
		<?php foreach($orderIds as $id):?>
			<?php $order = Mage::getModel("sales/order")->load($id);?>
			<?php 
				$prevOrders = Mage::getModel("sales/order")->getCollection()
					->addFieldToFilter('customer_id', array('eq'=>$customerId))
					->addFieldToFilter('state', array('in'=>array('complete','processing','new')))
					->addFieldToFilter('created_at',array('lt'=>$order->getCreatedAt()))
					->addFieldToFilter('entity_id', array('neq'=>$id));
					//echo count($prevOrders);
			?>
			<?php $createdAt = strtotime($order->getCreatedAt()); ?>
			<div class="head" style="clear:both;font-size:16pt;height:60px;"><div class="fl" style="line-height:40px;font-size:18pt"><!-- <img src="<?php //echo $this->getUrl('admin_zgb/br/render',array('code'=>$order->getIncrementId(),'text'=>1))?>"/> -->
					订单编号：#<?php echo $order->getIncrementId();?>&nbsp;&nbsp;<?php if(count($prevOrders)==0) echo "***首次下单***";?></div> <div class="fr" style="line-height:30px;">订购日期：<?php echo date("Y年m月d日 H:i", $createdAt+3600*8);?> <br/>发货日期：<?php echo date("Y年m月d日 H:i",time()+3600*8);?> <a href="javascript:window.print();" class="actions"> 打印</a></div></div>
			<?php $items = $order->getItemsCollection();?>
			<?php if($order->getShippingId()):?>
			<div class="head" style="text-align: left !important;">
				<div class="fl">
				<?php $shipping = Mage::getModel('tms/shipping')->load($order->getShippingId());?>
				<?php if($shipping->getIncrementId()) echo "运单号：".$shipping->getIncrementId();?>
				<?php $shipOrder = Mage::getModel('tms/shipping_order')->load($order->getIncrementId(),'increment_id');?>
				<?php if($shipOrder && $shipOrder->getSort()) echo "&nbsp;配送排序：".$shipOrder->getSort();?>
				<?php if($shipping->getCar()) echo "&nbsp;&nbsp;车辆信息：".$shipping->getCar();?>
				<?php if($shipping->getRegion()) echo "&nbsp;&nbsp;拣货区域：".$shipping->getRegion();?>
				</div>
			</div>
			<?php endif;?>
			<table class="data-table">
				<tr>
					<td>商品名</td>
					<td width="8%">SKU</td>
					<td width="12%">订购数量</td>
					<td width="8%">单价</td>
					<td width="13%">小计</td>
				</tr>
				<?php foreach($items as $item):?>
				<tr>
					<td style="text-align: left !important;"><?php if($item->getGiftId()) echo "**赠品**"; elseif($item->getExchange()) echo "**积分兑换**";?><?php echo $item->getName(); ?></td>
					<td style="text-align: left !important;"><?php echo $item->getSku(); ?></td>
					<td><?php echo intval($item->getQtyOrdered()); ?></td>
					<?php if($item->getGiftId()):?>
					<td><?php echo $_coreHelper->formatPrice(0); ?></td>
					<td><?php echo $_coreHelper->formatPrice(0); ?></td>
					<?php else:?>
					<td><?php echo $_coreHelper->formatPrice($item->getPrice()); ?></td>
					<td><?php echo $_coreHelper->formatPrice($item->getRowTotal()); ?></td>
					<?php endif;?>
				</tr>
				<?php endforeach;?>
				<?php if($order->getGrandTotal() != $order->getSubtotal()):?>
				<tr>
					<td colspan="4" align="right" style="text-align:right" align="right">合计</td>
					<td><?php echo $_coreHelper->formatPrice($order->getSubtotal(),false); ?></td>
				</tr>
				<?php endif;?>
				<?php if($order->getRewardpointsDiscount()>0):?>
				<tr>
					<td colspan="4" align="right" style="text-align:right" align="right">积分抵扣</td>
					<td><?php echo $_coreHelper->formatPrice($order->getRewardpointsDiscount(),false); ?></td>
				</tr>
				<?php  endif;?>
				<?php 
					$credit = 0;
					// echo $order->getCouponCode();
					$coupon = Mage::getModel('salesrule/coupon')->loadByCode($order->getCouponCode());
					if($coupon && $coupon->getId())
					{
						$rule = Mage::getModel('salesrule/rule')->load($coupon->getRuleId());
						//echo $rule->getId();
						if($rule && $rule->getId())
						{
							$credit = $rule->getDiscountAmount();
						}
					}
				?>
				<?php if($credit):?>
				<tr>
					<td colspan="4" align="right" style="text-align:right" align="right">抵扣</td>
					<td>-<?php echo $_coreHelper->formatPrice($credit,false); ?></td>
				</tr>
				<?php endif;?>
				<?php if($order->getDiscountAmount()!=0):?>
				<tr>
					<td colspan="4" align="right" style="text-align:right" align="right">其他折扣</td>
					<td><?php echo $_coreHelper->formatPrice($order->getDiscountAmount()+$credit,false); ?></td>
				</tr>
				<?php endif;?>
				<tr>
					<td colspan="4" align="right" style="text-align:right !important" align="right">总计</td>
					<td><?php echo $_coreHelper->formatPrice($order->getGrandTotal(),false); ?></td>
				</tr>
			</table>
			
			<!-- <div class="signature" style="clear:both">
				<div class="item" style="width:100%"><span class="label" style="width:260px">对货品生产日期有无异议：</span><span class="label" style="width:30px">有</span><span class="placeholder" style="width:30px">&nbsp;</span><span class="label" style="width:30px">无</span><span class="placeholder" style="width:30px">&nbsp;</span></div>
			</div> -->

			<?php
			//$order->setStatus('pending_processing');
			$order->setPrint(1);
			if($order->getState()!='canceled' && $order->getState()!='closed')
			{
				$order->save();
			}
			
		?>
		<?php endforeach;?>
		<div class="address">
			<?php $customer = Mage::getModel('customer/customer')->load($customerId); ?>
			配送地址：<?php echo $order->getShippingAddress()->format("oneline")." （".$customer->getLastname()."）";?>
			<?php
				$storeInfo = Mage::helper('qslb')->getStore($customer->getId());
				if($storeInfo && isset($storeInfo['addressDescription']) && $storeInfo['addressDescription'])
				{
					echo "</br>备注：{$storeInfo['addressDescription']}";
				}
			?>
		</div>
		<div class="signature" style="clear:both">
			<div class="item"><span class="label" style="width:auto">配货人：</span><span class="placeholder">&nbsp;</span></div>
			<div class="item"><span class="label" style="width:auto">送货人：</span><span class="placeholder">&nbsp;</span></div>
			<div class="item"><span class="label" style="width:auto">财务：</span><span class="placeholder">&nbsp;</span></div>
			<div class="item"><span class="label" style="width:auto">收货人：</span><span class="placeholder">&nbsp;</span></div>
		</div>
			<?php if(Mage::getStoreConfig('general/website/company_phone', $store)):?>
				<div class="clearfix"></div>
				<div class="address-bar" style="margin-top:10px">
					<span>联系电话：<?php echo Mage::getStoreConfig('general/website/company_phone', $store);?></span>
				</div>
			<?php endif;?>
			<?php if(Mage::getStoreConfig('general/website/company_warehouse_print_label', $store)):?>
				<div class="clearfix"></div>
				<div style="margin-top:10px;line-height:120%">
					<p><span>签收提示：</span><?php echo Mage::getStoreConfig('general/website/company_warehouse_print_label', $store);?></p>
				</div>
			<?php endif;?>
		</div>
	<?php endforeach;?>
	<?php endif; //if $websiteId = 0; ?>

</div>